<?php
//控制器

class home extends mysql{
    function userhome(){
        header("Content-Type: text/html;charset=utf8");
        $id = $_SESSION["id"];
        //留言内容查询
        //$sql = "select * from message where sn = 0 and p_id = ".$id.' or g_id = '.$id;
        $sql = 'select * from message where p_id = '.$id.' or g_id = '.$id.' and sn = 0 order by time desc;';
        $data = $this->select_all($sql);
        //重组数组，将发送人和接收人装入
        if(is_array($data) && !empty($data)){
            foreach($data as $i => $j){
                $aql1 = "select username from user_table where id = ".$j['p_id'];
                $user1 = $this->select_one($aql1);
                $data[$i]['put_username'] = $user1['username'];

                $aql2 = "select username from user_table where id = ".$j['g_id'];
                $user2 = $this->select_one($aql2);
                $data[$i]['get_username'] = $user2['username'];
            }
        }

        //修改
        if (isset($_REQUEST['update1'])) {
            if (!empty($_POST)) {
                $url = $this->config;
                $_SESSION['up_id'] = $_POST['id'];
                $url .= 'm=index&c=update&f=userup';
                header("Refresh: 0; url = $url");
                exit();
            }
        }

        //删除
        if (isset($_REQUEST['delete'])) {
            if (!empty($_POST)) {
                $id = $_POST['id'];
                $table = 'message';
                $where = "id = $id";
                //"DELETE FROM `test`.`message` WHERE  `id` = $id;";
                $ints = $this->delete($table, $where);
                if ($ints == true) {
                    $this->alert_str("删除成功。");
                } else {
                    $this->alert_str("删除失败。");
                }
            }
        }

        $this->display('index','index','home',['url' =>$this->config,'data'=>$data,'_SESSION' => $_SESSION]);
    }

}